package api

import (
	"context"
	"github.com/gin-gonic/gin"
	"wzgames/internal/api"
	"wzgames/internal/app"
	"wzgames/internal/client"
	"wzgames/internal/gloader"
	"wzgames/internal/gvalidator"
	"wzgames/pkg/server/http"
	"wzgames/pkg/server/http/middleware"
)

func RunServer() error {
	bootc := gloader.BootConfig()
	return app.Start(bootc,
		app.BeforeStart(func(ctx context.Context) error {
			return gvalidator.InitAgentHashIds()
		}),
		app.Server(http.NewHttpServer(bootc.Node().HttpConf,
			http.Use(middleware.Logger(), middleware.Gate(), middleware.Recovery(), middleware.NoCache(), middleware.Options(), middleware.Secure()),
			http.Route(func(g *gin.Engine) {
				api.InitRoute(g)
			}),
		)),
		app.AfterStop(func(ctx context.Context) error {
			client.CloseCatClient()
			client.CloseDouWalletClient()
			client.CloseBetTowerClient()
			gloader.Res().Stop()
			return nil
		}),
	)
}
